package com.speedata

import android.os.Bundle
import android.support.v7.app.AppCompatActivity

import android.util.Log
import android.view.KeyEvent
import android.view.View
import android.widget.Button
import android.widget.Toast
import com.speedata.uhftest.R
import java.io.BufferedWriter
import java.io.File
import java.io.FileWriter
import java.io.IOException

/**
 * @author xuyan
 */
class SwitchXActivity : AppCompatActivity(), View.OnClickListener {

    private var bt1: Button? = null
    private var bt2: Button? = null
    private var bt3: Button? = null
    private var bt4: Button? = null
    private var bt5: Button? = null
    private var bt6: Button? = null
    private var bt7: Button? = null
    private var bt8: Button? = null
    private var bt9: Button? = null
    private var bt10: Button? = null
    private var bt11: Button? = null
    private var bt12: Button? = null
    private var bt13: Button? = null
    private var bt14: Button? = null
    private var bt15: Button? = null
    private var bt16: Button? = null
    private var bt21: Button? = null
    private var bt22: Button? = null
    private var bt23: Button? = null
    private var bt24: Button? = null
    private var bt25: Button? = null
    private var bt26: Button? = null
    private var bt27: Button? = null
    private var bt28: Button? = null


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_switch_x)
        initView()
    }

    private fun initView() {
        bt1 = findViewById(R.id.bt1)
        bt2 = findViewById(R.id.bt2)
        bt3 = findViewById(R.id.bt3)
        bt4 = findViewById(R.id.bt4)
        bt5 = findViewById(R.id.bt5)
        bt6 = findViewById(R.id.bt6)
        bt7 = findViewById(R.id.bt7)
        bt8 = findViewById(R.id.bt8)
        bt9 = findViewById(R.id.bt9)
        bt10 = findViewById(R.id.bt10)
        bt11 = findViewById(R.id.bt11)
        bt12 = findViewById(R.id.bt12)
        bt13 = findViewById(R.id.bt13)
        bt14 = findViewById(R.id.bt14)
        bt15 = findViewById(R.id.bt15)
        bt16 = findViewById(R.id.bt16)
        bt21 = findViewById(R.id.bt21)
        bt22 = findViewById(R.id.bt22)
        bt23 = findViewById(R.id.bt23)
        bt24 = findViewById(R.id.bt24)
        bt25 = findViewById(R.id.bt25)
        bt26 = findViewById(R.id.bt26)
        bt27 = findViewById(R.id.bt27)
        bt28 = findViewById(R.id.bt28)


        bt1!!.setOnClickListener(this)
        bt2!!.setOnClickListener(this)
        bt3!!.setOnClickListener(this)
        bt4!!.setOnClickListener(this)
        bt5!!.setOnClickListener(this)
        bt6!!.setOnClickListener(this)
        bt7!!.setOnClickListener(this)
        bt8!!.setOnClickListener(this)
        bt9!!.setOnClickListener(this)
        bt10!!.setOnClickListener(this)
        bt11!!.setOnClickListener(this)
        bt12!!.setOnClickListener(this)
        bt13!!.setOnClickListener(this)
        bt14!!.setOnClickListener(this)
        bt15!!.setOnClickListener(this)
        bt16!!.setOnClickListener(this)
        bt21!!.setOnClickListener(this)
        bt22!!.setOnClickListener(this)
        bt23!!.setOnClickListener(this)
        bt24!!.setOnClickListener(this)
        bt25!!.setOnClickListener(this)
        bt26!!.setOnClickListener(this)
        bt27!!.setOnClickListener(this)
        bt28!!.setOnClickListener(this)
    }


    override fun onClick(v: View) {
        when (v.id) {
            R.id.bt1 -> if ("0on" == bt1!!.text.toString()) {
                open("0off")
                bt1!!.text = "0off"
                Toast.makeText(this, "0off", Toast.LENGTH_SHORT).show()
            } else {
                open("0on")
                bt1!!.text = "0on"
                Toast.makeText(this, "0on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt2 -> if ("1on" == bt2!!.text.toString()) {
                open("1off")
                bt2!!.text = "1off"
                Toast.makeText(this, "1off", Toast.LENGTH_SHORT).show()
            } else {
                open("1on")
                bt2!!.text = "1on"
                Toast.makeText(this, "1on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt3 -> if ("2on" == bt3!!.text.toString()) {
                open("2off")
                bt3!!.text = "2off"
                Toast.makeText(this, "2off", Toast.LENGTH_SHORT).show()
            } else {
                open("2on")
                bt3!!.text = "2on"
                Toast.makeText(this, "2on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt4 -> if ("3on" == bt4!!.text.toString()) {
                open("3off")
                bt4!!.text = "3off"
                Toast.makeText(this, "3off", Toast.LENGTH_SHORT).show()
            } else {
                open("3on")
                bt4!!.text = "3on"
                Toast.makeText(this, "3on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt5 -> if ("4on" == bt5!!.text.toString()) {
                open("4off")
                bt5!!.text = "4off"
                Toast.makeText(this, "4off", Toast.LENGTH_SHORT).show()
            } else {
                open("4on")
                bt5!!.text = "4on"
                Toast.makeText(this, "4on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt6 -> if ("5on" == bt6!!.text.toString()) {
                open("5off")
                bt6!!.text = "5off"
                Toast.makeText(this, "5off", Toast.LENGTH_SHORT).show()
            } else {
                open("5on")
                bt6!!.text = "5on"
                Toast.makeText(this, "5on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt7 -> if ("6on" == bt7!!.text.toString()) {
                open("6off")
                bt7!!.text = "6off"
                Toast.makeText(this, "6off", Toast.LENGTH_SHORT).show()
            } else {
                open("6on")
                bt7!!.text = "6on"
                Toast.makeText(this, "6on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt8 -> if ("7on" == bt8!!.text.toString()) {
                open("7off")
                bt8!!.text = "7off"
                Toast.makeText(this, "7off", Toast.LENGTH_SHORT).show()
            } else {
                open("7on")
                bt8!!.text = "7on"
                Toast.makeText(this, "7on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt9 -> if ("8on" == bt9!!.text.toString()) {
                open("8off")
                bt9!!.text = "8off"
                Toast.makeText(this, "8off", Toast.LENGTH_SHORT).show()
            } else {
                open("8on")
                bt9!!.text = "8on"
                Toast.makeText(this, "8on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt10 -> if ("9on" == bt10!!.text.toString()) {
                open("9off")
                bt10!!.text = "9off"
                Toast.makeText(this, "9off", Toast.LENGTH_SHORT).show()
            } else {
                open("9on")
                bt10!!.text = "9on"
                Toast.makeText(this, "9on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt11 -> if ("10on" == bt11!!.text.toString()) {
                open("10off")
                bt11!!.text = "10off"
                Toast.makeText(this, "10off", Toast.LENGTH_SHORT).show()
            } else {
                open("10on")
                bt11!!.text = "10on"
                Toast.makeText(this, "10on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt12 -> if ("11on" == bt12!!.text.toString()) {
                open("11off")
                bt12!!.text = "11off"
                Toast.makeText(this, "11off", Toast.LENGTH_SHORT).show()
            } else {
                open("11on")
                bt12!!.text = "11on"
                Toast.makeText(this, "11on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt13 -> if ("12on" == bt13!!.text.toString()) {
                open("12off")
                bt13!!.text = "12off"
                Toast.makeText(this, "12off", Toast.LENGTH_SHORT).show()
            } else {
                open("12on")
                bt13!!.text = "12on"
                Toast.makeText(this, "12on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt14 -> if ("13on" == bt14!!.text.toString()) {
                open("13off")
                bt14!!.text = "13off"
                Toast.makeText(this, "13off", Toast.LENGTH_SHORT).show()
            } else {
                open("13on")
                bt14!!.text = "13on"
                Toast.makeText(this, "13on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt15 -> if ("14on" == bt15!!.text.toString()) {
                open("14off")
                bt15!!.text = "14off"
                Toast.makeText(this, "14off", Toast.LENGTH_SHORT).show()
            } else {
                open("14on")
                bt15!!.text = "14on"
                Toast.makeText(this, "14on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt16 -> if ("15on" == bt16!!.text.toString()) {
                open("15off")
                bt16!!.text = "15off"
                Toast.makeText(this, "15off", Toast.LENGTH_SHORT).show()
            } else {
                open("15on")
                bt16!!.text = "15on"
                Toast.makeText(this, "15on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt21 -> if ("0on" == bt21!!.text.toString()) {
                open9524("0off")
                bt21!!.text = "0off"
                Toast.makeText(this, "0off", Toast.LENGTH_SHORT).show()
            } else {
                open9524("0on")
                bt21!!.text = "0on"
                Toast.makeText(this, "0on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt22 -> if ("1on" == bt22!!.text.toString()) {
                open9524("1off")
                bt22!!.text = "1off"
                Toast.makeText(this, "1off", Toast.LENGTH_SHORT).show()
            } else {
                open9524("1on")
                bt22!!.text = "1on"
                Toast.makeText(this, "1on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt23 -> if ("2on" == bt23!!.text.toString()) {
                open9524("2off")
                bt23!!.text = "2off"
                Toast.makeText(this, "2off", Toast.LENGTH_SHORT).show()
            } else {
                open9524("2on")
                bt23!!.text = "2on"
                Toast.makeText(this, "2on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt24 -> if ("3on" == bt24!!.text.toString()) {
                open9524("3off")
                bt24!!.text = "3off"
                Toast.makeText(this, "3off", Toast.LENGTH_SHORT).show()
            } else {
                open9524("3on")
                bt24!!.text = "3on"
                Toast.makeText(this, "3on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt25 -> if ("4on" == bt25!!.text.toString()) {
                open9524("4off")
                bt25!!.text = "4off"
                Toast.makeText(this, "4off", Toast.LENGTH_SHORT).show()
            } else {
                open9524("4on")
                bt25!!.text = "4on"
                Toast.makeText(this, "4on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt26 -> if ("5on" == bt26!!.text.toString()) {
                open9524("5off")
                bt26!!.text = "5off"
                Toast.makeText(this, "5off", Toast.LENGTH_SHORT).show()
            } else {
                open9524("5on")
                bt26!!.text = "5on"
                Toast.makeText(this, "5on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt27 -> if ("6on" == bt27!!.text.toString()) {
                open9524("6off")
                bt27!!.text = "6off"
                Toast.makeText(this, "6off", Toast.LENGTH_SHORT).show()
            } else {
                open9524("6on")
                bt27!!.text = "6on"
                Toast.makeText(this, "6on", Toast.LENGTH_SHORT).show()
            }
            R.id.bt28 -> if ("7on" == bt28!!.text.toString()) {
                open9524("7off")
                bt28!!.text = "7off"
                Toast.makeText(this, "7off", Toast.LENGTH_SHORT).show()
            } else {
                open9524("7on")
                bt28!!.text = "7on"
                Toast.makeText(this, "7on", Toast.LENGTH_SHORT).show()
            }
            else -> {
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        Toast.makeText(this@SwitchXActivity, keyCode.toString(), Toast.LENGTH_SHORT).show()
        Log.d("onkeydown", keyCode.toString())
        return super.onKeyDown(keyCode, event)
    }

    companion object {


        /**
         * 补光灯
         *
         * @param str
         */
        fun open(str: String) {
            Log.d("", "-print-rece-openCloseFlash $str start")
            val TorchFileName = File("/sys/class/misc/aw9523/gpio")
            //aw9523
            try {
                TorchFileWrite = BufferedWriter(FileWriter(TorchFileName, false))
                TorchFileWrite!!.write(str)
                TorchFileWrite!!.flush()
                TorchFileWrite!!.close()
            } catch (e: IOException) {
                e.printStackTrace()
                Log.d("", e.message)
            }

            Log.d("", "-print-rece-" + "openCloseFlash end")
        }

        private var TorchFileWrite: BufferedWriter? = null

        /**
         * 补光灯
         *
         * @param str
         */
        fun open9524(str: String) {
            Log.d("", "-print-rece-openCloseFlash $str start")
            val TorchFileName = File("/sys/class/misc/aw9524/gpio")
            //aw9523
            try {
                TorchFileWrite = BufferedWriter(FileWriter(TorchFileName, false))
                TorchFileWrite!!.write(str)
                TorchFileWrite!!.flush()
                TorchFileWrite!!.close()
            } catch (e: IOException) {
                e.printStackTrace()
                Log.d("", e.message)
            }

            Log.d("", "-print-rece-" + "openCloseFlash end")
        }
    }
}
